package com.rytong.tools.ui;

import java.util.Calendar;
import java.util.TimeZone;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.text.TextPaint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

public class LPDateField extends Component {
    public String attrName_;
    String attrUpdate_;
    public String attrHref_;
    String attrValue_;
    boolean attrSave_;
    private final String sunday_ = "星期天";
    private final String tuesday_ = "星期二";
    private final String monday_ = "星期一";
    private final String wednesday_ = "星期三";
    private final String thursday_ = "星期四";
    private final String friday_ = "星期五";
    private final String saturday_ = "星期六";

    private static final int BORDER_INDENT = 2;

    final static int MINHEHGHT = 50;
    final static int MINWIDTH = 200;

    public class MyLPDateField extends TextView implements CompositedComponent {

        private TextPaint paint_;

        public MyLPDateField(Context context) {
            super(context);
            setTextSize(SIZE_SMALL);
            paint_ = getPaint();
            paint_.setAntiAlias(true);
            paint_.setTextSize(SIZE_SMALL);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            // draw background.
            int bgColor = 0;
            CssStyle cssStyle = cssStyle();
            if (cssStyle != null) {
                bgColor = bgColor();
            }
            if (bgColor != 0) {
                paint_.setColor(bgColor);
                paint_.setStyle(Style.FILL);
                canvas.drawRect(0, 0, width_, height_, paint_);
            }
            // draw text.
            int textColor = Color.BLACK;
            if (cssStyle != null) {
                textColor = fgColor();
            }
            float textSize = getTextSize();
            paint_.setTextSize(textSize);
            paint_.setColor(textColor);
            float y = height_ / 2 + 1.0f * textSize / 2;
            String showString = getContentText();
            canvas.drawText(showString, 0, y, paint_);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:
                viewLPDateFieldTag_ = name();
                break;
            default:
                return super.onKeyDown(keyCode, event);
            }
            return true;
        }

        @Override
        public Component composited() {
            return LPDateField.this;
        }

        public boolean onTouchEvent(MotionEvent event) {
            boolean isAction = false;
            int touchEvent = event.getAction();
            switch (touchEvent) {
            case MotionEvent.ACTION_DOWN:
                yDown_ = event.getY();
                isAction = true;
                isAction = super.onTouchEvent(event);
                break;
            case MotionEvent.ACTION_MOVE:
                moveY(yDown_, event.getY());
                break;
            default:
                isAction = super.onTouchEvent(event);
            }
            return isAction;
        }
    }

    public String getContentText() {
        MyLPDateField temp = (MyLPDateField) realView_;
        CharSequence cs = temp.getText();
        if (null == cs || cs.equals("")) {
            // final Calendar c = Calendar.getInstance();
            // c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            // return createDateString(c);
            return "";
        }
        return temp.getText().toString().substring(0, 8);
    }

    /**
     * @param bv
     * @param label
     * @param value
     * @param required
     * @param constraints
     */
    public LPDateField(Activity bv, String label, String value, boolean required) {
        realView_ = new MyLPDateField(bv);
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        String dateStr = createDateString(c);
        setContentText(dateStr);
    }

    /**
     * @param year
     * @param month
     * @param day
     */
    public void setContentText(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        String dateStr = createDateString(cal);
        setContentText(dateStr);
    }

    /**
     * 
     * @param c
     * @return
     */
    private String createDateString(final Calendar c) {
        String yearStr;
        String monthStr;
        String dayStr;
        yearStr = c.get(Calendar.YEAR) + "";
        // 月份从0算起，所以应该+1处理
        monthStr = c.get(Calendar.MONTH) + 1 + "";
        if (((c.get(Calendar.MONTH) + 1) < 10) && (monthStr.length() < 2)) {
            monthStr = "0".concat(monthStr);
        }
        dayStr = c.get(Calendar.DAY_OF_MONTH) + "";
        if (dayStr.length() < 2) {
            dayStr = "0".concat(dayStr);
        }
        String dateStr = yearStr.concat(monthStr).concat(dayStr);
        String dayOfWeekStr = getDayOfWeek(c);
        if (dayOfWeekStr != null) {
            dateStr = dateStr.concat(" " + dayOfWeekStr);
        }
        return dateStr;
    }

    /**
     * @param cal
     * @return
     */
    private String getDayOfWeek(Calendar cal) {
        if (cal == null) {
            return null;
        }
        String dayOfWeekStr = null;
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
        case Calendar.SUNDAY:
            dayOfWeekStr = sunday_;
            break;
        case Calendar.MONDAY:
            dayOfWeekStr = monday_;
            break;
        case Calendar.TUESDAY:
            dayOfWeekStr = tuesday_;
            break;
        case Calendar.WEDNESDAY:
            dayOfWeekStr = wednesday_;
            break;
        case Calendar.THURSDAY:
            dayOfWeekStr = thursday_;
            break;
        case Calendar.FRIDAY:
            dayOfWeekStr = friday_;
            break;
        case Calendar.SATURDAY:
            dayOfWeekStr = saturday_;
            break;
        default:
            break;
        }
        return dayOfWeekStr;
    }

    @Override
    public void initRealView(Activity activity, String value) {
        // TODO Auto-generated method stub
        realView_ = new MyLPDateField(activity);
    }
    
    /*************************************************************************
     * Set and Get methods.
     *************************************************************************/
}
